<template>
  <div class="home-container">
    <el-header class="header">
      <div class="logo">美食外卖</div>
      <div class="search-bar">
        <el-input placeholder="搜索餐厅、美食" v-model="searchKeyword" class="search-input">
          <el-button slot="append" icon="el-icon-search" @click="searchRestaurants"></el-button>
        </el-input>
      </div>
      <div class="nav-links">
        <router-link to="/order" class="nav-link">
          <i class="el-icon-s-order"></i> 我的订单
        </router-link>
        <router-link to="/user" class="nav-link">
          <i class="el-icon-user"></i> 个人中心
        </router-link>
        <div class="cart-link" @click="$router.push('/order')">
          <el-badge :value="cartCount" class="cart-badge">
            <i class="el-icon-shopping-cart-2"></i> 购物车
          </el-badge>
        </div>
        <template v-if="isLoggedIn">
          <span class="welcome">欢迎，{{ username }}</span>
          <el-button type="text" @click="logout">退出</el-button>
        </template>
        <template v-else>
          <router-link to="/login" class="nav-link">登录</router-link> /
          <router-link to="/register" class="nav-link">注册</router-link>
        </template>
      </div>
    </el-header>
    
    <div class="main-content">
      <!-- 分类过滤 -->
      <div class="category-filter">
        <el-button 
          v-for="category in categories" 
          :key="category"
          :class="['category-btn', selectedCategory === category ? 'active' : '']"
          @click="filterByCategory(category)"
        >
          {{ category }}
        </el-button>
      </div>

      <!-- 餐厅列表 -->
      <div class="restaurant-list">
        <el-card 
          v-for="restaurant in filteredRestaurants" 
          :key="restaurant.id" 
          class="restaurant-card"
          @click.native="goToRestaurant(restaurant.id)"
        >
          <div class="restaurant-image">
            <img :src="restaurant.image" alt="餐厅图片">
          </div>
          <div class="restaurant-info">
            <h3>{{ restaurant.name }}</h3>
            <div class="restaurant-rating">
              <i class="el-icon-star-on"></i>
              <span>{{ restaurant.rating }}</span>
              <span class="monthly-sales">月售 {{ restaurant.monthlySales }} 单</span>
            </div>
            <div class="restaurant-price">
              <span>起送 ¥{{ restaurant.minOrderAmount }}</span>
              <span class="divider">|</span>
              <span>配送费 ¥{{ restaurant.deliveryFee }}</span>
            </div>
            <div class="restaurant-desc">{{ restaurant.description }}</div>
          </div>
        </el-card>

        <div v-if="loading" class="loading-more">
          <i class="el-icon-loading"></i> 加载中...
        </div>
        <div v-if="!loading && filteredRestaurants.length === 0" class="no-data">
          没有找到相关餐厅
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  name: 'Home',
  data() {
    return {
      restaurants: [],
      loading: false,
      searchKeyword: '',
      selectedCategory: '全部',
      categories: ['全部', '川菜', '粤菜', '烧烤', '日料', '西餐', '快餐'],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    ...mapGetters(['isLoggedIn', 'cartCount']),
    username() {
      return this.$store.state.userInfo.username || ''
    },
    filteredRestaurants() {
      if (this.selectedCategory === '全部') {
        return this.restaurants
      }
      return this.restaurants.filter(r => r.category === this.selectedCategory)
    }
  },
  created() {
    this.fetchRestaurants()
  },
  methods: {
    ...mapActions(['logout']),
    fetchRestaurants() {
      this.loading = true
      this.$http.get('/restaurant/list', {
        params: {
          page: this.currentPage,
          size: this.pageSize,
          category: this.selectedCategory === '全部' ? '' : this.selectedCategory,
          keyword: this.searchKeyword
        }
      }).then(response => {
        const { data } = response.data
        this.restaurants = data.records
        this.total = data.total
      }).catch(error => {
        this.$message.error('获取餐厅列表失败')
        console.error('获取餐厅列表失败', error)
      }).finally(() => {
        this.loading = false
      })
    },
    filterByCategory(category) {
      this.selectedCategory = category
      this.currentPage = 1
      this.fetchRestaurants()
    },
    searchRestaurants() {
      this.currentPage = 1
      this.fetchRestaurants()
    },
    goToRestaurant(id) {
      this.$router.push(`/restaurant/${id}`)
    }
  }
}
</script>

<style scoped>
.home-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
  height: 60px !important;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
}

.search-bar {
  width: 400px;
}

.nav-links {
  display: flex;
  align-items: center;
}

.nav-link, .cart-link {
  margin: 0 10px;
  color: #606266;
  cursor: pointer;
}

.nav-link:hover, .cart-link:hover {
  color: #409EFF;
}

.cart-badge {
  margin-top: 10px;
}

.welcome {
  margin-right: 10px;
  color: #606266;
}

.main-content {
  padding: 20px;
  flex: 1;
  background-color: #f5f7fa;
}

.category-filter {
  margin-bottom: 20px;
  background-color: #fff;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.category-btn {
  margin-right: 10px;
  margin-bottom: 10px;
}

.category-btn.active {
  color: #409EFF;
  border-color: #409EFF;
}

.restaurant-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.restaurant-card {
  cursor: pointer;
  transition: transform 0.3s;
}

.restaurant-card:hover {
  transform: translateY(-5px);
}

.restaurant-image {
  height: 160px;
  overflow: hidden;
}

.restaurant-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.restaurant-info {
  padding: 10px 0;
}

.restaurant-info h3 {
  margin: 0 0 10px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.restaurant-rating {
  color: #FF9900;
  margin-bottom: 5px;
}

.monthly-sales {
  color: #999;
  margin-left: 10px;
}

.restaurant-price {
  font-size: 12px;
  color: #666;
  margin-bottom: 5px;
}

.divider {
  margin: 0 5px;
}

.restaurant-desc {
  font-size: 12px;
  color: #999;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading-more, .no-data {
  grid-column: 1 / -1;
  text-align: center;
  padding: 20px;
  color: #909399;
}
</style>
